import datetime,random

class TOOLS():

    def __init__(self):
        self.v = 3.0

    def timeNow(self):
        time_ms = datetime.datetime.now().strftime("%Y%m%d%H%M%S.%f")
        return float(time_ms)


    def convert(self,size, box):#box[x1,x2,y1,y2],box[iw,ih]
        # rx:  归一化的中心点x坐标计算公式：(x2 + x1) / 2.0 / iw
        # ry:  归一化的中心点y坐标计算公式：(y2 + y1) / 2.0 / ih
        # rw/2:归一化的目标框宽度的计算公式：(x2 - x1)/ 2.0 / iw
        # rh/2:归一化的目标框高度的计算公式：(y2 - y1)/ 2.0 / ih

        # x2 = (rx+rw/2.0)*iw
        # x1 = (rx-rw/2.0)*iw
        # y2 = (ry+rh/2.0)*ih
        # y1 = (ry1-h/2.0)*ih

        dw = 1. / size[0]
        dh = 1. / size[1]

        x = (box[0] + box[1]) / 2.0
        y = (box[2] + box[3]) / 2.0
        w = box[1] - box[0]
        h = box[3] - box[2]
        x = x * dw
        w = w * dw
        y = y * dh
        h = h * dh
        return x, y, w, h

    def convertR(self,rx, ry, rw, rh, iw, ih):
        # rx:  归一化的中心点x坐标计算公式：(x2 + x1) / 2.0 / iw
        # ry:  归一化的中心点y坐标计算公式：(y2 + y1) / 2.0 / ih
        # rw/2:归一化的目标框宽度的计算公式：(x2 - x1)/ 2.0 / iw
        # rh/2:归一化的目标框高度的计算公式：(y2 - y1)/ 2.0 / ih
        # rx = rx * iw
        # ry = ry * ih
        # rw = rw * iw
        # rh = rh * ih


        # x2 = (rx + rw / 2.0)
        # x1 = (rx - rw / 2.0)
        # y2 = (ry + rh / 2.0)
        # y1 = (ry - rh / 2.0)

        x2 = (rx + rw / 2.0) * iw
        x1 = (rx - rw / 2.0) * iw
        y2 = (ry + rh / 2.0) * ih
        y1 = (ry - rh / 2.0) * ih

        # print(x1,x2,y1,y2)
        x, y, w, h = x1, y1, x2 - x1, y2 - y1
        return map(int,[x, y, w, h])
        # return x1,x2,y1,y2

    def versionMsg(self):
        with open("version.txt","r")as f:
            lines = f.readlines()
            for line in lines:
                if(line.startswith("version")):
                    version = line.split(":")[-1].rstrip()
        return version
        #         if(line.startswith("name")):
        #             name = line.split(":")[-1].rstrip()
        # version = {"version":"版本V"+version,"name":name}
        # return version

    def lossTime(self):
        if(int(self.v) != float(self.versionMsg()) ):
            return random.randint(7, 9)
        else:
            return 0

    def getTime(self):
        # if(int(self.v) != float(self.versionMsg()) ):
        return random.randint(43, 51)


toolsClass = TOOLS()